<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <link href="jquery/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css" type="text/css"
          rel="stylesheet"/>

    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.js"></script>
    <script type="text/javascript"
            src="jquery/bootstrap-datetimepicker-master/locale/bootstrap-datetimepicker.zh-CN.js"></script>

    <link rel="stylesheet" type="text/css" href="jquery/bs_pagination/jquery.bs_pagination.min.css">
    <script type="text/javascript" src="jquery/bs_pagination/jquery.bs_pagination.min.js"></script>
    <script type="text/javascript" src="jquery/bs_pagination/en.js"></script>


    <script type="text/javascript">

        $(function () {

            $(".time").datetimepicker({
                minView: "month",
                language: 'zh-CN',
                format: 'yyyy-mm-dd',
                autoclose: true,
                todayBtn: true,
                pickerPosition: "bottom-left"
            });



            //页面加载完毕后触发一个方法
            //默认展开列表的第一页，每页展现两条记录
            pageList(1, 5, "all");

            //为全选的复选框绑定事件，触发全选操作
            $("#qx").click(function () {

                $("input[name=xz]").prop("checked", this.checked);

            })

            $("#followBody").on("click", $("input[name=xz]"), function () {

                $("#qx").prop("checked", $("input[name=xz]").length == $("input[name=xz]:checked").length);

            })

            //为删除按钮绑定事件，执行市场活动删除操作
            $("#deleteBtn").click(function () {

                //找到复选框中所有挑√的复选框的jquery对象
                var $xz = $("input[name=xz]:checked");

                if ($xz.length == 0) {

                    alert("请选择需要删除的记录");

                    //肯定选了，而且有可能是1条，有可能是多条
                } else {


                    if (confirm("确定删除所选中的记录吗？")) {

                        //url:workbench/activity/delete.do?id=xxx&id=xxx&id=xxx

                        //拼接参数
                        var param = "";

                        //将$xz中的每一个dom对象遍历出来，取其value值，就相当于取得了需要删除的记录的id
                        for (var i = 0; i < $xz.length; i++) {

                            param += "id=" + $($xz[i]).val();

                            //如果不是最后一个元素，需要在后面追加一个&符
                            if (i < $xz.length - 1) {

                                param += "&";

                            }

                        }

                        //alert(param);
                        $.ajax({

                            url: "workbench/activity/delete.do",
                            data: param,
                            type: "post",
                            dataType: "json",
                            success: function (data) {

                                /*

                                    data
                                        {"success":true/false}

                                 */
                                if (data.success) {

                                    //删除成功后
                                    //回到第一页，维持每页展现的记录数
                                    pageList(1, $("#followPage").bs_pagination('getOption', 'rowsPerPage'));


                                } else {

                                    alert("删除市场活动失败");

                                }


                            }

                        })


                    }


                }


            })


        });

        function pageList(pageNo, pageSize, time) {

            //将全选的复选框的√干掉
            $("#qx").prop("checked", false);

            var date = new Date();
            var choose = "";

            var year = date.getUTCFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();

            var year2 = year;
            var month2 = month;
            var day2 = day;
            var now = "";


           if(time == "thisYear"){
               year2+=1;
            }

            if(time == "thisMonth"){
                month2+=1;
            }

            if(time == "today"){
                day2+=1;
            }

            if(time != "all"){
                now += year2;
            }

            if(time=="lastMonth" || time=="thisMonth"){
                now = now + "-";
                if (month2 < 10) {
                    now = now + "0";
                }
                 now = now + month2;
            }

            if(time=="lastWeek" || time=="yesterday" || time=="today"){
                now = now + "-";
                if (month2 < 10) {
                    now = now + "0";
                }
                now = now + month2 + "-";
                if (day2 < 10) {
                    now = now + "0";
                }
                now = now + day2;
            }

            if (time == "lastYear") {
                year -= 1;
            }
            if (time == "lastMonth") {
                if (month <= 1) {
                    year -= 1;
                    month = 12;
                }else {
                    month -= 1;
                }

            }
            if (time == "lastWeek") {

                if (month == 1 && day <= 7) {
                    year -= 1;
                    month = 12;
                    day = 31 - day + 1;
                } else if ((month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11) && day <= 7) {
                    month -= 1;
                    day = 31 - day + 1;
                } else if ((month == 5 || month == 7 || month == 10 || month == 12) && day <= 7) {
                    month -= 1;
                    day = 30 - day + 1;
                } else if ((year % 4 == 0) && month == 3 && day <= 7) { // 闰年 2月
                    month -= 1;
                    day = 29 - day + 1;
                } else if ((year % 4 != 0) && month == 3 && day <= 7) { // 非闰年 2月
                    month -= 1;
                    day = 28 - day + 1;
                } else {
                    day = day - 7;
                }


            }
            if (time == "yesterday") {

                if (month == 1 && day <= 1) {
                    year -= 1;
                    month = 12;
                    day = 31 - day + 1;
                } else if ((month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11) && day <= 1) {
                    month -= 1;
                    day = 31 - day + 1;
                } else if ((month == 5 || month == 7 || month == 10 || month == 12) && day <= 1) {
                    month -= 1;
                    day = 30 - day + 1;
                } else if ((year % 4 == 0) && month == 3 && day <= 1) { // 闰年 2月
                    month -= 1;
                    day = 29 - day + 1;
                } else if ((year % 4 != 0) && month == 3 && day <= 1) { // 非闰年 2月
                    month -= 1;
                    day = 28 - day + 1;
                } else {
                    day = day - 1;
                }

            }

            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }

            if(time!="all"){
                choose += year;
            }

            if(time=="lastMonth" || time=="thisMonth"){
                choose = choose + "-" + month;
            }

            if(time=="lastWeek" || time=="yesterday" || time=="today"){
                choose = choose + "-" + month + "-" + day;
            }

            $.ajax({

                url: "workbench/transaction/pageList2.do",
                data: {

                    "pageNo": pageNo,
                    "pageSize": pageSize,
                    "choose": choose,
                    "now" : now

                },
                type: "get",
                dataType: "json",
                success: function (data) {

                    var html = "";

                    $.each(data.dataList, function (i, n) {

                        html += '<tr class="active">';
                        html += '<td><input type="checkbox" name="xz" value="' + n.id + '"/></td>';
                        html += '<td>' + n.tranId + '</td>';
                        html += '<td>' + n.contactSummary + '</td>';
                        html += '<td>' + n.nextContactTime + '</td>';
                        html += '<td>' + n.contactWay + '</td>';
                        html += '<td>' + n.state + '</td>';
                        html += '<td>' + n.createTime + '</td>';
                        html += '<td><a style="text-decoration: none; cursor: pointer;" href="javascript:void(0);" onclick="deleteById(\'' + n.id + '\')"> 删除</a></td>';
                        html += '</tr>';

                    })

                    $("#followBody").html(html);

                    //计算总页数
                    var totalPages = data.total % pageSize == 0 ? data.total / pageSize : parseInt(data.total / pageSize) + 1;

                    //数据处理完毕后，结合分页查询，对前端展现分页信息
                    $("#followPage").bs_pagination({
                        currentPage: pageNo, // 页码
                        rowsPerPage: pageSize, // 每页显示的记录条数
                        maxRowsPerPage: 20, // 每页最多显示的记录条数
                        totalPages: totalPages, // 总页数
                        totalRows: data.total, // 总记录条数

                        visiblePageLinks: 3, // 显示几个卡片

                        showGoToPage: true,
                        showRowsPerPage: true,
                        showRowsInfo: true,
                        showRowsDefaultInfo: true,

                        //该回调函数时在，点击分页组件的时候触发的
                        onChangePage: function (event, data) {
                            pageList(data.currentPage, data.rowsPerPage,time);
                        }
                    });


                }

            })

        }

        function deleteById(id) {

            if(confirm("确定删除吗")){

                $.ajax({

                    url: "workbench/transaction/deleteFollow.do",
                    data: {
                        "id" : id
                    },
                    type: "post",
                    dataType: "json",
                    success: function (data) {

                        if (data.success) {

                            pageList(1, $("#followPage").bs_pagination('getOption', 'rowsPerPage'),"all");

                        } else {

                            alert("删除跟进失败");

                        }

                    }

                })

            }

        }

    </script>
</head>
<body>

    <input type="hidden" id="hidden-createTime"/>

    <div>
        <div style="position: relative; left: 10px; top: -10px;">
            <div class="page-header">
                <h3>跟进列表</h3>
            </div>
        </div>
    </div>
    <div style="position: relative; top: -20px; left: 0px; width: 100%; height: 100%;">
    <div style="width: 100%; position: absolute;top: 5px; left: 10px;">

        <div class="btn-toolbar " role="toolbar" style="height: 80px;">

                <div class="form-group">
                    <div class="input-group">
                        <span style="size: 15px">选择时间</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:void(0);" style="text-decoration: none; cursor: pointer;" onclick="pageList(1,5,'lastYear')" >去年</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:void(0);" style="text-decoration: none; cursor: pointer;" onclick="pageList(1,5,'thisYear')">今年</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:void(0);" style="text-decoration: none; cursor: pointer;" onclick="pageList(1,5,'lastMonth')">上个月</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:void(0);" style="text-decoration: none; cursor: pointer;" onclick="pageList(1,5,'thisMonth')" >本月</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:void(0);" style="text-decoration: none; cursor: pointer;" onclick="pageList(1,5,'lastWeek')">上周</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:void(0);" style="text-decoration: none; cursor: pointer;" onclick="pageList(1,5,'yesterday')">昨天</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:void(0);" style="text-decoration: none; cursor: pointer;" onclick="pageList(1,5,'today')">今天</a>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:void(0);" style="text-decoration: none; cursor: pointer;" onclick="pageList(1,5,'all')">总数</a>

                    </div>
                </div>


        </div>

        <div style="position: relative;top: 10px;">
            <table class="table table-hover">
                <thead>
                <tr style="color: #B3B3B3;">
                    <td><input type="checkbox" id="qx"/></td>
                    <td>交易名称</td>
                    <td>最新跟进记录</td>
                    <td>下次联系时间</td>
                    <td>联系方式</td>
                    <td>状态</td>
                    <td>实际跟进时间</td>
                    <td></td>
                </tr>
                </thead>
                <tbody id="followBody">

                </tbody>
            </table>
        </div>

        <div style="height: 50px; position: relative;top: 30px;">

            <div id="followPage"></div>

        </div>
    </div>

</div>
</body>
</html>